package bean;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.validation.constraints.NotNull;

import org.primefaces.event.SelectEvent;

import dao.JPA;
import dao.TreatmentActDAO;
import dao.TreatmentRequestDAO;
import entity.Doctor;
import entity.TreatmentRequest;

@Named
@ConversationScoped
public class CreateTreatmentRequestBean implements Serializable {

	@Inject
	private Conversation conversation;
	@Inject @JPA
	private TreatmentRequestDAO requestDAO;
	@Inject @JPA
	private TreatmentActDAO treatmentActDAO;
	private TreatmentRequest treatmentRequest = new TreatmentRequest();	
	private int actId;
	private String cid;
	@NotNull(message="You must select doctor")
	private String doctorName;
	private List<Integer> hourList = new ArrayList<>();
	private List<Integer> minuteList = new ArrayList<>();
	@NotNull(message="You must set meet time")
	private Integer selectedHour;
	@NotNull(message="You must set meet time")
	private Integer selectedMinute;
	
	@PostConstruct
	private void init() {			
		if (conversation.isTransient()) 
			conversation.begin();
		cid = conversation.getId();		
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> params = context.getExternalContext().getRequestParameterMap();
		if(params.get("actId") != null) { 	
			actId = Integer.valueOf(params.get("actId"));
			treatmentRequest.setAct(treatmentActDAO.getEntityById(actId));
		}	
		for(int i = 8; i < 18; i++)
			hourList.add(i);
		for(int i = 0; i < 60; i += 15) 			
			minuteList.add(i);
	}
	
	public void onRowSelect(SelectEvent event) {				
		treatmentRequest.setDoctor((Doctor) event.getObject());
		doctorName = treatmentRequest.getDoctor().getFirstName() + " " + treatmentRequest.getDoctor().getLastName();
		try {			
			FacesContext.getCurrentInstance().getExternalContext().redirect("createTreatmentRequest.xhtml?actId=" + actId + "&cid=" + cid);
		} catch(IOException ioe) {
			ioe.printStackTrace();
		}				
	}
	
	public String createTreatmentRequest() {
		requestDAO.persist(treatmentRequest);
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Treatment request created", "Treatment request created"));
		conversation.end();
		return "viewTreatmentAct.xhtml?actId=" + actId + "&faces-redirect=true";
	}
	
	public String goBack() {
		conversation.end();
		return "viewTreatmentAct.xhtml?actId=" + actId + "&faces-redirect=true";
	}

	public TreatmentRequest getTreatmentRequest() {		
		return treatmentRequest;
	}

	public void setTreatmentRequest(TreatmentRequest treatmentRequest) {
		this.treatmentRequest = treatmentRequest;
	}

	public Conversation getConversation() {
		return conversation;
	}

	public void setConversation(Conversation conversation) {
		this.conversation = conversation;
	}

	public String getDoctorName() {
		return doctorName;
	}

	public void setDoctorName(String doctorName) {
		this.doctorName = doctorName;
	}

	public int getActId() {
		return actId;
	}

	public void setActId(int actId) {
		this.actId = actId;
	}

	public Integer getSelectedHour() {	
		return selectedHour;
	}

	public void setSelectedHour(Integer selectedHour) {
		this.selectedHour = selectedHour;
		treatmentRequest.getMeetDate().set(Calendar.HOUR_OF_DAY, selectedHour);
	}

	public Integer getSelectedMinute() {
		return selectedMinute;
	}

	public void setSelectedMinute(Integer selectedMinute) {
		this.selectedMinute = selectedMinute;
		treatmentRequest.getMeetDate().set(Calendar.MINUTE, selectedMinute);
	}

	public List<Integer> getHourList() {
		return hourList;
	}

	public List<Integer> getMinuteList() {
		return minuteList;
	}
	
}
